package com.hd.assistant.dal.dao.ibatis;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.hd.assistant.dal.dao.ClickedDetailDAO;
import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.query.ClickedDetailQuery;
import com.hd.assistant.dal.dataobject.ClickedDetail;

public class IbatisClickedDetailDao extends SqlMapClientDaoSupport implements
		ClickedDetailDAO {

	public void insertClickedDetail(ClickedDetail item) throws DAOException {
		if(item == null)
			return ;
		try{
			this.getSqlMapClientTemplate().insert(
				"ClickedDetail.insertClickedDetailDO", item);
		}catch(DataAccessException e){
			throw new DAOException("IbatisClickedDetailDao.insertClickedDetail", e);
		}
	}

	public ClickedDetail getNearestByClickedId(String clickedId) throws DAOException{
		if(clickedId == null )
			return null;
		try{
			Object o =this.getSqlMapClientTemplate().queryForObject("ClickedDetail.selectNearestDO", clickedId);
			if(o != null)
				return (ClickedDetail)o;
		}catch(DataAccessException e){
			throw new DAOException("IbatisClickedDetailDao.getNearestByClickedId", e);
		}
		return null;
	}

	public int updateClickedNumsByItemidAndDate(String clickedId,String clickedDate,int needToaddNum)
			throws DAOException {
		if(clickedId == null || 
				clickedDate == null || 
						needToaddNum == 0){
			return 0;
		}
		ClickedDetailQuery query = new ClickedDetailQuery();
		query.setClickedId(clickedId);
		query.setClickedDate(clickedDate);
		query.setNeedToAddNum(needToaddNum);
		try{
			return this.getSqlMapClientTemplate().update("ClickedDetail.updateClickedNumByItemAndClickedDate", query);
		}catch(DataAccessException e){
			throw new DAOException("IbatisClickedDetailDao.updateClickedNumsByItemidAndDate", e);
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<ClickedDetail> getAllDetailsByQuery (ClickedDetailQuery query)throws DAOException{
		if(query == null)
			return null;

		try{
			return (List<ClickedDetail>)this.getSqlMapClientTemplate().queryForList("ClickedDetail.selectByQuery", query);
		}catch(DataAccessException e){
			throw new DAOException("IbatisClickedDetailDao.getAllDetailsByItemId", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<ClickedDetail> getAllDetailsGroupByByItemidAndUserId(
			ClickedDetailQuery query) throws DAOException {
		if(query == null)
			return null;

		try{
			return (List<ClickedDetail>)this.getSqlMapClientTemplate().queryForList("ClickedDetail.selectGroupByUserIdAndItem", query);
		}catch(DataAccessException e){
			throw new DAOException("IbatisClickedDetailDao.getAllDetailsGroupByByItemidAndUserId", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<ClickedDetail> getAllDetailsGroupByByClickedDateAndUserId(
			ClickedDetailQuery query) throws DAOException {
		if(query == null)
			return null;

		try{
			return (List<ClickedDetail>)this.getSqlMapClientTemplate().queryForList("ClickedDetail.selectGroupByUserIdAndClickedDate", query);
		}catch(DataAccessException e){
			throw new DAOException("IbatisClickedDetailDao.getAllDetailsGroupByByClickedDateAndUserId", e);
		}
	}
}
